// RedJey Graphics User Interface Library for J2ME Platform.
// Copyright (C) 2008 Yevgen Krapiva
//
// This library is free software; you can redistribute
// it and/or modify it under the terms of the
// GNU Lesser General Public License as published
// by the Free Software Foundation; either version 3.0
// of the License, or any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//

package com.incrediblesoft.redjey;

/**
 * @author Yevgen Krapiva<br>
 *         Homepage: http://code.google.com/p/redjey/<br>
 *         Feedback: ykrapiva@gmail.com<br>
 *         Copyright (C) 2008 Yevgen Krapiva<br>
 */

import javax.microedition.lcdui.Graphics;
import java.util.Hashtable;


public class TextPane extends TextComponent
{
    protected String[] splittedText, tempSplittedText;
    protected int maxLineWidth, tempMaxLineWidth;
    protected int numCols;//, numRows;

    /**
     * Constructs a new TextPane with specified text.
     * @param text Text
     */
    public TextPane(String text)
    {
        this(text, null);
    }

    /**
     * Constructs a new TextPane with specified text and custom style class.
     * @param text Text
     * @param styleClassName Style class name
     */
    public TextPane(String text, String styleClassName)
    {
        this(text, 0, /*0, */styleClassName);
    }

    /**
     * Constructs a new TextPane with specified text.
     * @param text Text
     * @param numCols Maximum number of characters the line can have
     * @param styleClassName Style class name
     */
    public TextPane(String text, int numCols, /*int numRows,*/ String styleClassName)
    {
        super(text, styleClassName);
        this.numCols = numCols;
        //this.numRows = numRows;
    }

    public String getName()
    {
        return "TextPane";
    }

    protected Dimension calcPreferredSize(int paddingHorizontal, int paddingVertical, CustomFont font, Hashtable attributes)
    {
        Dimension dim = new Dimension();

        if (numCols > 0)
            tempSplittedText = CustomFont.splitString(text, font, 0, numCols);
        else
            tempSplittedText = CustomFont.splitString(text, font, 0, 0);

        tempMaxLineWidth = 0;
        for (int i=0; i<tempSplittedText.length; i++)
            tempMaxLineWidth = Math.max(tempMaxLineWidth, font.stringWidth(tempSplittedText[i]));
        dim.width = tempMaxLineWidth;

        dim.height = /*numRows > 0 ? font.getHeight() * numRows : */tempSplittedText.length * font.getHeight();

        return dim;
    }

    protected void calcContentSize()
    {
        if (splittedText != null)
        {
            contentSize.width = maxLineWidth;
            contentSize.height = currentStyle.font.getHeight() * splittedText.length;
        }
        else
            super.calcContentSize();
    }


    protected void validateComponent()
    {
        //System.out.println("TextPane clinetSize("+clientSize.width+", "+clientSize.height+")");
        if (clientSize.width == tempMaxLineWidth)
        {
            splittedText = tempSplittedText;
            maxLineWidth = tempMaxLineWidth;
        }
        else
        {
            splittedText = CustomFont.splitString(text, currentStyle.font, clientSize.width, 0);
            maxLineWidth = 0;
            for (int i=0; i< splittedText.length; i++)
                maxLineWidth = Math.max(maxLineWidth, currentStyle.font.stringWidth(splittedText[i]));
        }

        tempSplittedText = null;
        tempMaxLineWidth = 0;

        //this.height = splittedText.length * currentStyle.font.getHeight() + currentStyle.getMinHeight();


        //else
        //    System.out.println("TextPane - cannot validate");
    }

    protected void applyContentAlignment(Graphics g)
    {
        g.translate(insets[0], insets[2]);
    } 

    protected void paintComponent(Graphics g)
    {
        //System.out.println("insets: "+insets[0]+" "+insets[1]+" "+insets[2]+" "+insets[3]);
        if (splittedText != null && splittedText.length > 0)
        {
            //g.translate(insets[0], insets[2]);

            if ((contentAlignment & HCENTER_ALIGNMENT) != 0)
            {
                g.translate(-(clientSize.width - maxLineWidth) / 2, 0);
            }
            else if ((contentAlignment & RIGHT_ALIGNMENT) != 0)
            {
                g.translate(-(clientSize.width - maxLineWidth), 0);
            }

            int clipY = g.getClipY();
            int clipH = g.getClipHeight();
            //int num = 0;
            int y;
            int h = currentStyle.font.getHeight();

            for (int i=0; i < splittedText.length; i++)
            {
                int offset_x = 0;

                if ((contentAlignment & HCENTER_ALIGNMENT) != 0)
                {
                    int thisLineWidth = currentStyle.font.stringWidth(splittedText[i]);
                    offset_x = (clientSize.width - thisLineWidth) / 2;
                }
                else if ((contentAlignment & RIGHT_ALIGNMENT) != 0)
                {
                    int thisLineWidth = currentStyle.font.stringWidth(splittedText[i]);
                    offset_x = clientSize.width - thisLineWidth;
                }

                y =  i * h;
                if ( !(y + h < clipY || y > clipY + clipH) )
                {
                    currentStyle.font.drawString(g, splittedText[i], offset_x, y, Graphics.LEFT | Graphics.TOP);
                    //num++;
                }
            }

           // System.out.println("Text pane drawn "+num+" lines of text");
        }
    }
}
